package br.com.quiz.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;

import br.com.quiz.dao.ProfessorDAO;
import br.com.quiz.domain.Professor;

@FacesConverter("professorConverter")
public class ProfessorConverter implements javax.faces.convert.Converter {

	@Override
	public Object getAsObject(FacesContext facesContext, UIComponent component,
			String valor) {
		try {
			Integer codigo = Integer.parseInt(valor);
			Professor professor = ProfessorDAO.filtrarPorId(codigo);
			System.out.println("Converteu");
			return professor;
		} catch (RuntimeException ex) {
			return null;
		}
	}

	@Override
	public String getAsString(FacesContext facesContext, UIComponent component,
			Object obj) {
		// TODO Auto-generated method stub
		try {
			Professor professor = (Professor) obj;
			Integer codigo = professor.getProId();
			return codigo.toString();
		} catch (RuntimeException ex) {
			return null;
		}
	}

}
